package com.zjsru.plan2023.other;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: CookLee
 * @Date: 2023/8/19
 * 回文排列
 * \
 * 输入："tactcoa"
 * 输出：true（排列有"tacocat"、"atcocta"，等等）
 */
public class CanPermutePalindrome {
    
    /**
     * 模拟
     *
     * @param s 年代
     * @return boolean
     */
    public boolean canPermutePalindrome(String s) {
        Map<Character, Integer> countMap = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            countMap.put(c,countMap.getOrDefault(c,0) + 1);
        }
        int cnt = 0;
        List<Integer> collect = countMap.values().stream().sorted((a,b) -> b.compareTo(a)).collect(Collectors.toList());
        for (Integer value : collect) {
            if (value % 2 == 0) {
            } else {
                cnt++;
                if(cnt > 1) {
                    return false;
                }
            }
        }
        return true;
    }
    
    public static void main(String[] args) {
        CanPermutePalindrome canPermutePalindrome = new CanPermutePalindrome();
        String s = "aab";
        System.out.println(canPermutePalindrome.canPermutePalindrome(s));
    }
}
